package com.bipa.controller;

import com.bipa.bean.User;
import com.bipa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@SessionAttributes(types = {User.class})
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String tologin(){
        return "front/login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ModelAndView login(String loginName, String passWord){
        ModelAndView mv = new ModelAndView();
        User user = userService.checkLogin(loginName,passWord);
        if (user!=null){
            mv.addObject("session_user",user);
            mv.setViewName("redirect:./index");
        }else {
            mv.addObject("msg","用户名或密码错误！");
            mv.setViewName("front/login");
        }
        return mv;
    }

    @RequestMapping("logout")
    public String logout(SessionStatus sessionStatus,HttpSession session){
        //session.removeAttribute("session_user");
        sessionStatus.setComplete();

        return "front/login";
    }
}
